package com.zhentao.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.dto.ResponseResult;
import com.zhentao.pojo.Brand;
import com.zhentao.pojo.Category;
import com.zhentao.pojo.Product;
import com.zhentao.service.BrandService;
import com.zhentao.service.CategoryService;
import com.zhentao.service.ProductService;
import com.zhentao.utils.MinioUtil;
import com.zhentao.vo.QueryParam;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/products")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private BrandService brandService;
    @Autowired
    private MinioUtil minioUtil;

    @RequestMapping("/findProductList")
    public Result findProductList(@RequestBody QueryParam queryParam){
        Page<Product> productList = productService.findProductList(queryParam);
        return Result.OK(productList);
    }

    @RequestMapping("/findCategorys")
    public Result findCategorys(){
        List<Category> list = categoryService.list();
        return Result.OK(list);
    }

    @RequestMapping("/findBrands")
    public Result findBrands(){
        List<Brand> list = brandService.list();
        return Result.OK(list);
    }

    @PostMapping("/luojiDel")
    public Result luojiDel(@RequestBody Map<String, Integer> params) {
        Integer productId = params.get("productId");
        if (productId == null) {
            return Result.ERROR("参数错误: productId不能为空");
        }

        // 创建更新对象
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("deleted", 1)  // 设置删除标记为1
                .eq("product_id", productId);

        // 执行更新
        boolean success = productService.update(updateWrapper);

        return success ? Result.OK("删除成功") : Result.ERROR("删除失败");
    }

    @RequestMapping("xiajia")
    public Result xiajia(@RequestParam Integer productId,@RequestParam Integer status){
        Product product = productService.getById(productId);
        product.setStatus(status);
        productService.updateById(product);
        return Result.OK("修改成功");
    }

    @PostMapping("addProduct")
    public Result addProduct(@RequestPart("product") Product product,
                             @RequestPart("file") MultipartFile file) {
        // 新增：商品名称内容审核
        ResponseResult checkResult = com.zhentao.utils.TextModerationPlusUtil.checkContent(product.getProductName());
        if (checkResult.getCode() != 600) {
            // 600为审核通过，其他为不通过
            return Result.ERROR("商品名称不合规：" + checkResult.getErrorMessage());
        }
        try {
            String imageUrl = minioUtil.upload(file);
            product.setAvatar(imageUrl);
            product.setCreateTime(new Date());
            product.setUpdateTime(new Date());
            product.setDeleted(0);
            productService.save(product);
            return Result.OK("添加成功");
        } catch (Exception e) {
            return Result.ERROR("图片上传失败: " + e.getMessage());
        }
    }

    @PostMapping("updateProduct")
    public Result updateProduct(@RequestPart("product") Product product,
                            @RequestPart(value = "file", required = false) MultipartFile file) {
    try {
        if (file != null && !file.isEmpty()) {
            String imageUrl = minioUtil.upload(file);
            product.setAvatar(imageUrl);
        }
        product.setUpdateTime(new Date());
        productService.updateById(product);
        return Result.OK("修改成功");
    } catch (Exception e) {
        return Result.ERROR("图片上传失败: " + e.getMessage());
    }}



}
